﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using QLKN.DataAccess;
using QLKN.GUI;
using System.Windows.Forms;

namespace QLKN.GUI
{
    public partial class frmMain : Form
    {
        string t;
        public frmMain(string tenDN)
        {
            InitializeComponent();
            sttTendangnhap.Text = "Xin chào " + tenDN;
            t= tenDN;
        }
        SqlConnection conn;

        public string LayTTDangnhap(string tenDN)
        {
            
            conn = Data.getConnection();
            string sql = @"SELECT pid FROM nhanvien WHERE nvtendangnhap='" + tenDN + "'";
            conn.Open();
            DataTable dt = Data.getDataTable(sql,conn);
            string i = dt.Rows[0]["pid"].ToString();
            conn.Close();
            return i;
        }
        

        public void Phanquyen(int pid)
        {
            switch (pid)
            {
                case 1: HienThiNghiepVu_Admin(); break;
                case 4: HienThiNghiepVu_TTKN(); break;

            }
        }
        public void HienThiNghiepVu_Admin()
        {
            menuTTKN.Enabled = false;
        }
        public void HienThiNghiepVu_Truongphong()
        {
            itemSaoluu.Enabled = false;
            itemKhoiphuc.Enabled = false;
            menuTTKN.Enabled = false;

        }
        public void HienThiNghiepVu_Nhanvien()
        {
            itemSaoluu.Enabled = false;
            itemKhoiphuc.Enabled = false;
            itemDoithongtincanhan.Enabled = true;
            
        }
        public void HienThiNghiepVu_TTKN()
        {
            itemSaoluu.Enabled = false;
            itemKhoiphuc.Enabled = false;
            menuQLNhanvien.Enabled = false;
            menuTruongphong.Enabled = false;
            menuNhanvien.Enabled = false;
               
        }
        public void HienThiNGhiepVu_TruongPhong()
        { 
            
        }
        public void HienThiNGhiepVu_Nhanvien()
        {

        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            int _pid =Convert.ToInt32(LayTTDangnhap(t));
            Phanquyen(_pid);
        }

        private void menuQLNhanvien_Click(object sender, EventArgs e)
        {
            frmQLNhanvien _frm= new frmQLNhanvien();
            _frm.MdiParent = this;
            _frm.Show();


        }

        private void itemDoithongtincanhan_Click(object sender, EventArgs e)
        {
            frmSuathongtincanhan _frmDoithongtin = new frmSuathongtincanhan(t);
            _frmDoithongtin.MdiParent = this;
            _frmDoithongtin.Show();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            frmQLKhachHang _frm = new frmQLKhachHang();
            _frm.MdiParent = this;
            _frm.Show();
        }

        private void qLSảnPhẩmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Quanlysanpham _frm = new Quanlysanpham();
            _frm.MdiParent = this;
            _frm.Show();
        }

        private void menuTTKN_Click(object sender, EventArgs e)
        {
            //Lapkhieunai _frm = new Lapkhieunai();
            //_frm.MdiParent = this;
            //_frm.Show();
        }

        private void itemDangxuat_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("Bạn có muốn thoát ???","THÔNG BÁO",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
            {
                this.Dispose();
            }

        }

        private void itemSaoluu_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn sao lưu CSDL không?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
            string ngay = DateTime.Now.Day.ToString();
            string thang = DateTime.Now.Month.ToString();
            string nam = DateTime.Now.Year.ToString();
            string gio = DateTime.Now.Hour.ToString() ;
            string phut = DateTime.Now.Minute.ToString();
            //string giay = DateTime.Now.Second.ToString();
                try
                {

                string path = "D:\\_Study\\Quan ly du an\\DatabaseBackup\\QLKN-"+ ngay + "-" + thang + "-" + nam+"("+gio+"h"+phut+").bak";
               // string path = "D:\\QLKN.bak";
                string sqlRestore = "BACKUP DATABASE [Qlgiaiquyetkhieunai] TO DISK = '" + path+"'";
                conn = Data.getConnection();
                conn.Open();

                SqlCommand cmd = new SqlCommand(sqlRestore, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Cơ sở dữ liệu đã được sao lưu ");

                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message, "Sao lưu CSDL");
                return;
                }

            }
        }

        private void itemKhoiphuc_Click(object sender, EventArgs e)
        {
            
        }

        private void itemTimkiemKH_Click(object sender, EventArgs e)
        {
            frmHoadon frm = new frmHoadon();
            frm.MdiParent = this;
            frm.Show();
        }

        

        
    }
}
